home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / MacPerl 506 appl folder.sit / MacPerl 506 appl folder / Mac_Perl_506r1m_appl / lib / perl5db.pl < prev    next >
Perl Script  |  1995-11-14  |  15KB  |  568 lines

  1. package DB;
  2.  
  3. # modified Perl debugger, to be run from Emacs in perldb-mode
  4. # Ray Lischner (uunet!mntgfx!lisch) as of 5 Nov 1990
  5. # Johan Vromans -- upgrade to 4.0 pl 10
  6.  
  7. $header = '$RCSfile: perl5db.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:07 $';
  8. #
  9. # This file is automatically included if you do perl -d.
  10. # It's probably not useful to include this yourself.
  11. #
  12. # Perl supplies the values for @line and %sub.  It effectively inserts
  13. # a &DB'DB(<linenum>); in front of every place that can
  14. # have a breakpoint.  It also inserts a do 'perldb.pl' before the first line.
  15. #
  16. # $Log:    perldb.pl,v $
  17.  
  18. local($^W) = 0;
  19.  
  20. if ($MacPerl::Version =~ /MPW/) {
  21.     $console = "Dev:Console";
  22. } else {
  23.     $console = "Dev:Console:Perl Debug";    # Separate window for application
  24. }
  25. $rcfile=".perldb";
  26.  
  27. open(IN, "<$console") || open(IN,  "<&STDIN");    # so we don't dingle stdin
  28. open(OUT,">$console") || open(OUT, ">&STDERR")
  29.     || open(OUT, ">&STDOUT");    # so we don't dongle stdout
  30. select(OUT);
  31. $| = 1;                # for DB::OUT
  32. select(STDOUT);
  33. $| = 1;                # for real STDOUT
  34. $sub = '';
  35.  
  36. # Is Perl being run from Emacs?
  37. # $emacs = $main::ARGV[0] eq '-emacs';
  38. # shift(@main::ARGV) if $emacs;
  39.  
  40. $header =~ s/.Header: ([^,]+),v(¥s+¥S+¥s+¥S+).*$/$1$2/;
  41. print OUT "¥nLoading DB routines from $header¥n";
  42. # print OUT ("Emacs support ",
  43. #        $emacs ? "enabled" : "available",
  44. #        ".¥n");
  45. print OUT "¥nEnter h for help.¥n¥n";
  46.  
  47. sub DB {
  48.     &save;
  49.     ($package, $filename, $line) = caller;
  50.     $usercontext = '($@, $!, $,, $/, $¥, $^W) = @saved;' .
  51.     "package $package;";        # this won't let them modify, alas
  52.     local(*dbline) = "::_<$filename";
  53.     $max = $#dbline;
  54.     if (($stop,$action) = split(/¥0/,$dbline{$line})) {
  55.     if ($stop eq '1') {
  56.         $signal |= 1;
  57.     }
  58.     else {
  59.         $evalarg = "¥$DB::signal |= do {$stop;}"; &eval;
  60.         $dbline{$line} =~ s/;9($|¥0)/$1/;
  61.     }
  62.     }
  63.     if ($single || $trace || $signal) {
  64.     if ($emacs) {
  65.         print OUT "¥032¥032$filename:$line:0¥n";
  66.     } else {
  67.         $prefix = $sub =~ /'|::/ ? "" : "${package}::";
  68.         $prefix .= "$sub($filename:";
  69.         if (length($prefix) > 30) {
  70.         print OUT "$prefix$line):¥n$line:¥t",$dbline[$line];
  71.         $prefix = "";
  72.         $infix = ":¥t";
  73.         }
  74.         else {
  75.         $infix = "):¥t";
  76.         print OUT "$prefix$line$infix",$dbline[$line];
  77.         }
  78.         for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
  79.         last if $dbline[$i] =~ /^¥s*(}|#|¥n)/;
  80.         print OUT "$prefix$i$infix",$dbline[$i];
  81.         }
  82.     }
  83.     }
  84.     $evalarg = $action, &eval if $action;
  85.     if ($single || $signal) {
  86.     $evalarg = $pre, &eval if $pre;
  87.     print OUT $#stack . " levels deep in subroutine calls!¥n"
  88.         if $single & 4;
  89.     $start = $line;
  90.       CMD:
  91.     while ((print OUT "  DB<", $#hist+1, "> "), $cmd=&gets) {
  92.         {
  93.         $single = 0;
  94.         $signal = 0;
  95.         $cmd eq '' && exit 0;
  96.         chop($cmd);
  97.         # This will remove the prompt picked up by MPW
  98.         $tmp_prompt = "  DB<" . ($#hist + 1) . "> ";
  99.         $cmd =~ s/^$tmp_prompt(.*)$/$1/;
  100.         # MPW Removal ended...
  101.         $cmd =~ s/¥¥$// && do {
  102.             print OUT "  cont: ";
  103.             $cmd .= &gets;
  104.             redo CMD;
  105.         };
  106.         $cmd =~ /^q$/ && exit 0;
  107.         $cmd =~ /^$/ && ($cmd = $laststep);
  108.         push(@hist,$cmd) if length($cmd) > 1;
  109.         ($i) = split(/¥s+/,$cmd);
  110.         eval "¥$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
  111.         $cmd =~ /^h$/ && do {
  112.             print OUT "
  113. T        Stack trace.
  114. s        Single step.
  115. n        Next, steps over subroutine calls.
  116. r        Return from current subroutine.
  117. c [line]    Continue; optionally inserts a one-time-only breakpoint 
  118.         at the specified line.
  119. <CR>        Repeat last n or s.
  120. l min+incr    List incr+1 lines starting at min.
  121. l min-max    List lines.
  122. l line        List line;
  123. l        List next window.
  124. -        List previous window.
  125. w line        List window around line.
  126. l subname    List subroutine.
  127. f filename    Switch to filename.
  128. /pattern/    Search forwards for pattern; final / is optional.
  129. ?pattern?    Search backwards for pattern.
  130. L        List breakpoints and actions.
  131. S        List subroutine names.
  132. t        Toggle trace mode.
  133. b [line] [condition]
  134.         Set breakpoint; line defaults to the current execution line; 
  135.         condition breaks if it evaluates to true, defaults to ¥'1¥'.
  136. b subname [condition]
  137.         Set breakpoint at first line of subroutine.
  138. d [line]    Delete breakpoint.
  139. D        Delete all breakpoints.
  140. a [line] command
  141.         Set an action to be done before the line is executed.
  142.         Sequence is: check for breakpoint, print line if necessary,
  143.         do action, prompt user if breakpoint or step, evaluate line.
  144. A        Delete all actions.
  145. V [pkg [vars]]    List some (default all) variables in package (default current).
  146. X [vars]    Same as ¥"V currentpackage [vars]¥".
  147. < command    Define command before prompt.
  148. > command    Define command after prompt.
  149. ! number    Redo command (default previous command).
  150. ! -number    Redo number¥'th to last command.
  151. H -number    Display last number commands (default all).
  152. q or ^D        Quit.
  153. p expr        Same as ¥"print DB::OUT expr¥" in current package.
  154. = [alias value]    Define a command alias, or list current aliases.
  155. command        Execute as a perl statement in current package.
  156.  
  157. ";
  158.             next CMD; };
  159.         $cmd =~ /^t$/ && do {
  160.             $trace = !$trace;
  161.             print OUT "Trace = ".($trace?"on":"off")."¥n";
  162.             next CMD; };
  163.         $cmd =~ /^S$/ && do {
  164.             foreach $subname (sort(keys %sub)) {
  165.             print OUT $subname,"¥n";
  166.             }
  167.             next CMD; };
  168.         $cmd =~ s/^X¥b/V $package/;
  169.         $cmd =~ /^V$/ && do {
  170.             $cmd = "V $package"; };
  171.         $cmd =~ /^V¥b¥s*(¥S+)¥s*(.*)/ && do {
  172.             local ($savout) = select(OUT);
  173.             $packname = $1;
  174.             @vars = split(' ',$2);
  175.             do 'dumpvar.pl' unless defined &main::dumpvar;
  176.             if (defined &main::dumpvar) {
  177.             &main::dumpvar($packname,@vars);
  178.             }
  179.             else {
  180.             print DB::OUT "dumpvar.pl not available.¥n";
  181.             }
  182.             select ($savout);
  183.             next CMD; };
  184.         $cmd =~ /^f¥b¥s*(.*)/ && do {
  185.             $file = $1;
  186.             if (!$file) {
  187.             print OUT "The old f command is now the r command.¥n";
  188.             print OUT "The new f command switches filenames.¥n";
  189.             next CMD;
  190.             }
  191.             if (!defined $main::{'_<' . $file}) {
  192.             if (($try) = grep(m#^_<.*$file#, keys %main::)) {
  193.                 $file = substr($try,2);
  194.                 print "¥n$file:¥n";
  195.             }
  196.             }
  197.             if (!defined $main::{'_<' . $file}) {
  198.             print OUT "There's no code here anything matching $file.¥n";
  199.             next CMD;
  200.             }
  201.             elsif ($file ne $filename) {
  202.             *dbline = "::_<$file";
  203.             $max = $#dbline;
  204.             $filename = $file;
  205.             $start = 1;
  206.             $cmd = "l";
  207.             } };
  208.         $cmd =~ /^l¥b¥s*([':A-Za-z_][':¥w]*)/ && do {
  209.             $subname = $1;
  210.             $subname = "main::" . $subname unless $subname =~ /'|::/;
  211.             $subname = "main" . $subname if substr($subname,0,1)eq "'";
  212.             $subname = "main" . $subname if substr($subname,0,2)eq "::";
  213.             ($file,$subrange) = split(/:/,$sub{$subname});
  214.             if ($file ne $filename) {
  215.             *dbline = "::_<$file";
  216.             $max = $#dbline;
  217.             $filename = $file;
  218.             }
  219.             if ($subrange) {
  220.             if (eval($subrange) < -$window) {
  221.                 $subrange =~ s/-.*/+/;
  222.             }
  223.             $cmd = "l $subrange";
  224.             } else {
  225.             print OUT "Subroutine $1 not found.¥n";
  226.             next CMD;
  227.             } };
  228.         $cmd =~ /^w¥b¥s*(¥d*)$/ && do {
  229.             $incr = $window - 1;
  230.             $start = $1 if $1;
  231.             $start -= $preview;
  232.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  233.         $cmd =~ /^-$/ && do {
  234.             $incr = $window - 1;
  235.             $cmd = 'l ' . ($start-$window*2) . '+'; };
  236.         $cmd =~ /^l$/ && do {
  237.             $incr = $window - 1;
  238.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  239.         $cmd =~ /^l¥b¥s*(¥d*)¥+(¥d*)$/ && do {
  240.             $start = $1 if $1;
  241.             $incr = $2;
  242.             $incr = $window - 1 unless $incr;
  243.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  244.         $cmd =~ /^l¥b¥s*(([¥d¥$¥.]+)([-,]([¥d¥$¥.]+))?)?/ && do {
  245.             $end = (!$2) ? $max : ($4 ? $4 : $2);
  246.             $end = $max if $end > $max;
  247.             $i = $2;
  248.             $i = $line if $i eq '.';
  249.             $i = 1 if $i < 1;
  250.             if ($emacs) {
  251.             print OUT "¥032¥032$filename:$i:0¥n";
  252.             $i = $end;
  253.             } else {
  254.             for (; $i <= $end; $i++) {
  255.                 print OUT "$i:¥t", $dbline[$i];
  256.                 last if $signal;
  257.             }
  258.             }
  259.             $start = $i;    # remember in case they want more
  260.             $start = $max if $start > $max;
  261.             next CMD; };
  262.         $cmd =~ /^D$/ && do {
  263.             print OUT "Deleting all breakpoints...¥n";
  264.             for ($i = 1; $i <= $max ; $i++) {
  265.             if (defined $dbline{$i}) {
  266.                 $dbline{$i} =~ s/^[^¥0]+//;
  267.                 if ($dbline{$i} =~ s/^¥0?$//) {
  268.                 delete $dbline{$i};
  269.                 }
  270.             }
  271.             }
  272.             next CMD; };
  273.         $cmd =~ /^L$/ && do {
  274.             for ($i = 1; $i <= $max; $i++) {
  275.             if (defined $dbline{$i}) {
  276.                 print OUT "$i:¥t", $dbline[$i];
  277.                 ($stop,$action) = split(/¥0/, $dbline{$i});
  278.                 print OUT "  break if (", $stop, ")¥n" 
  279.                 if $stop;
  280.                 print OUT "  action:  ", $action, "¥n" 
  281.                 if $action;
  282.                 last if $signal;
  283.             }
  284.             }
  285.             next CMD; };
  286.         $cmd =~ /^b¥b¥s*([':A-Za-z_][':¥w]*)¥s*(.*)/ && do {
  287.             $subname = $1;
  288.             $cond = $2 || '1';
  289.             $subname = "${package}::" . $subname
  290.             unless $subname =~ /'|::/;
  291.             $subname = "main" . $subname if substr($subname,0,1) eq "'";
  292.             $subname = "main" . $subname if substr($subname,0,2) eq "::";
  293.             ($filename,$i) = split(/:/, $sub{$subname});
  294.             $i += 0;
  295.             if ($i) {
  296.             *dbline = "::_<$filename";
  297.             ++$i while $dbline[$i] == 0 && $i < $#dbline;
  298.             $dbline{$i} =~ s/^[^¥0]*/$cond/;
  299.             } else {
  300.             print OUT "Subroutine $subname not found.¥n";
  301.             }
  302.             next CMD; };
  303.         $cmd =~ /^b¥b¥s*(¥d*)¥s*(.*)/ && do {
  304.             $i = ($1?$1:$line);
  305.             $cond = $2 || '1';
  306.             if ($dbline[$i] == 0) {
  307.             print OUT "Line $i not breakable.¥n";
  308.             } else {
  309.             $dbline{$i} =~ s/^[^¥0]*/$cond/;
  310.             }
  311.             next CMD; };
  312.         $cmd =~ /^d¥b¥s*(¥d+)?/ && do {
  313.             $i = ($1?$1:$line);
  314.             $dbline{$i} =~ s/^[^¥0]*//;
  315.             delete $dbline{$i} if $dbline{$i} eq '';
  316.             next CMD; };
  317.         $cmd =~ /^A$/ && do {
  318.             for ($i = 1; $i <= $max ; $i++) {
  319.             if (defined $dbline{$i}) {
  320.                 $dbline{$i} =~ s/¥0[^¥0]*//;
  321.                 delete $dbline{$i} if $dbline{$i} eq '';
  322.             }
  323.             }
  324.             next CMD; };
  325.         $cmd =~ /^<¥s*(.*)/ && do {
  326.             $pre = action($1);
  327.             next CMD; };
  328.         $cmd =~ /^>¥s*(.*)/ && do {
  329.             $post = action($1);
  330.             next CMD; };
  331.         $cmd =~ /^a¥b¥s*(¥d+)(¥s+(.*))?/ && do {
  332.             $i = $1;
  333.             if ($dbline[$i] == 0) {
  334.             print OUT "Line $i may not have an action.¥n";
  335.             } else {
  336.             $dbline{$i} =~ s/¥0[^¥0]*//;
  337.             $dbline{$i} .= "¥0" . action($3);
  338.             }
  339.             next CMD; };
  340.         $cmd =~ /^n$/ && do {
  341.             $single = 2;
  342.             $laststep = $cmd;
  343.             last CMD; };
  344.         $cmd =~ /^s$/ && do {
  345.             $single = 1;
  346.             $laststep = $cmd;
  347.             last CMD; };
  348.         $cmd =~ /^c¥b¥s*(¥d*)¥s*$/ && do {
  349.             $i = $1;
  350.             if ($i) {
  351.             if ($dbline[$i] == 0) {
  352.                 print OUT "Line $i not breakable.¥n";
  353.                 next CMD;
  354.             }
  355.             $dbline{$i} =~ s/(¥0|$)/;9$1/;    # add one-time-only b.p.
  356.             }
  357.             for ($i=0; $i <= $#stack; ) {
  358.             $stack[$i++] &= ~1;
  359.             }
  360.             last CMD; };
  361.         $cmd =~ /^r$/ && do {
  362.             $stack[$#stack] |= 2;
  363.             last CMD; };
  364.         $cmd =~ /^T$/ && do {
  365.             local($p,$f,$l,$s,$h,$a,@a,@sub);
  366.             for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  367.             @a = ();
  368.             for $arg (@args) {
  369.                 $_ = "$arg";
  370.                 s/'/¥¥'/g;
  371.                 s/([^¥0]*)/'$1'/
  372.                 unless /^(?: -?[¥d.]+ | ¥*[¥w:]* )$/x;
  373.                 s/([¥200-¥377])/sprintf("M-%c",ord($1)&0177)/eg;
  374.                 s/([¥0-¥37¥177])/sprintf("^%c",ord($1)^64)/eg;
  375.                 push(@a, $_);
  376.             }
  377.             $w = $w ? '@ = ' : '$ = ';
  378.             $a = $h ? '(' . join(', ', @a) . ')' : '';
  379.             push(@sub, "$w$s$a from file $f line $l¥n");
  380.             last if $signal;
  381.             }
  382.             for ($i=0; $i <= $#sub; $i++) {
  383.             last if $signal;
  384.             print OUT $sub[$i];
  385.             }
  386.             next CMD; };
  387.         $cmd =~ /^¥/(.*)$/ && do {
  388.             $inpat = $1;
  389.             $inpat =~ s:([^¥¥])/$:$1:;
  390.             if ($inpat ne "") {
  391.             eval '$inpat =~ m'."¥a$inpat¥a";    
  392.             if ($@ ne "") {
  393.                 print OUT "$@";
  394.                 next CMD;
  395.             }
  396.             $pat = $inpat;
  397.             }
  398.             $end = $start;
  399.             eval '
  400.             for (;;) {
  401.             ++$start;
  402.             $start = 1 if ($start > $max);
  403.             last if ($start == $end);
  404.             if ($dbline[$start] =~ m'."¥a$pat¥a".'i) {
  405.                 if ($emacs) {
  406.                 print OUT "¥032¥032$filename:$start:0¥n";
  407.                 } else {
  408.                 print OUT "$start:¥t", $dbline[$start], "¥n";
  409.                 }
  410.                 last;
  411.             }
  412.             } ';
  413.             print OUT "/$pat/: not found¥n" if ($start == $end);
  414.             next CMD; };
  415.         $cmd =~ /^¥?(.*)$/ && do {
  416.             $inpat = $1;
  417.             $inpat =~ s:([^¥¥])¥?$:$1:;
  418.             if ($inpat ne "") {
  419.             eval '$inpat =~ m'."¥a$inpat¥a";    
  420.             if ($@ ne "") {
  421.                 print OUT "$@";
  422.                 next CMD;
  423.             }
  424.             $pat = $inpat;
  425.             }
  426.             $end = $start;
  427.             eval '
  428.             for (;;) {
  429.             --$start;
  430.             $start = $max if ($start <= 0);
  431.             last if ($start == $end);
  432.             if ($dbline[$start] =~ m'."¥a$pat¥a".'i) {
  433.                 if ($emacs) {
  434.                 print OUT "¥032¥032$filename:$start:0¥n";
  435.                 } else {
  436.                 print OUT "$start:¥t", $dbline[$start], "¥n";
  437.                 }
  438.                 last;
  439.             }
  440.             } ';
  441.             print OUT "?$pat?: not found¥n" if ($start == $end);
  442.             next CMD; };
  443.         $cmd =~ /^!+¥s*(-)?(¥d+)?$/ && do {
  444.             pop(@hist) if length($cmd) > 1;
  445.             $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
  446.             $cmd = $hist[$i] . "¥n";
  447.             print OUT $cmd;
  448.             redo CMD; };
  449.         $cmd =~ /^!(.+)$/ && do {
  450.             $pat = "^$1";
  451.             pop(@hist) if length($cmd) > 1;
  452.             for ($i = $#hist; $i; --$i) {
  453.             last if $hist[$i] =~ $pat;
  454.             }
  455.             if (!$i) {
  456.             print OUT "No such command!¥n¥n";
  457.             next CMD;
  458.             }
  459.             $cmd = $hist[$i] . "¥n";
  460.             print OUT $cmd;
  461.             redo CMD; };
  462.         $cmd =~ /^H¥b¥s*(-(¥d+))?/ && do {
  463.             $end = $2?($#hist-$2):0;
  464.             $hist = 0 if $hist < 0;
  465.             for ($i=$#hist; $i>$end; $i--) {
  466.             print OUT "$i: ",$hist[$i],"¥n"
  467.                 unless $hist[$i] =~ /^.?$/;
  468.             };
  469.             next CMD; };
  470.         $cmd =~ s/^p( .*)?$/print DB::OUT$1/;
  471.         $cmd =~ /^=/ && do {
  472.             if (local($k,$v) = ($cmd =~ /^=¥s*(¥S+)¥s+(.*)/)) {
  473.             $alias{$k}="s~$k~$v~";
  474.             print OUT "$k = $v¥n";
  475.             } elsif ($cmd =~ /^=¥s*$/) {
  476.             foreach $k (sort keys(%alias)) {
  477.                 if (($v = $alias{$k}) =~ s~^s¥~$k¥~(.*)¥~$~$1~) {
  478.                 print OUT "$k = $v¥n";
  479.                 } else {
  480.                 print OUT "$k¥t$alias{$k}¥n";
  481.                 };
  482.             };
  483.             };
  484.             next CMD; };
  485.         }
  486.         $evalarg = $cmd; &eval;
  487.         print OUT "¥n";
  488.     }
  489.     if ($post) {
  490.         $evalarg = $post; &eval;
  491.     }
  492.     }
  493.     ($@, $!, $,, $/, $¥) = @saved;
  494. }
  495.  
  496. sub save {
  497.     @saved = ($@, $!, $,, $/, $¥, $^W);
  498.     $, = ""; $/ = "¥n"; $¥ = ""; $^W = 0;
  499. }
  500.  
  501. # The following takes its argument via $evalarg to preserve current @_
  502.  
  503. sub eval {
  504.     eval "$usercontext $evalarg; &DB::save";
  505.     print OUT $@;
  506. }
  507.  
  508. sub action {
  509.     local($action) = @_;
  510.     while ($action =~ s/¥¥$//) {
  511.     print OUT "+ ";
  512.     $action .= &gets;
  513.     }
  514.     $action;
  515. }
  516.  
  517. sub gets {
  518.     local($.);
  519.     <IN>;
  520. }
  521.  
  522. sub catch {
  523.     $signal = 1;
  524. }
  525.  
  526. sub sub {
  527.     push(@stack, $single);
  528.     $single &= 1;
  529.     $single |= 4 if $#stack == $deep;
  530.     if (wantarray) {
  531.     @i = &$sub;
  532.     $single |= pop(@stack);
  533.     @i;
  534.     }
  535.     else {
  536.     $i = &$sub;
  537.     $single |= pop(@stack);
  538.     $i;
  539.     }
  540. }
  541.  
  542. $trace = $signal = $single = 0;    # uninitialized warning suppression
  543.  
  544. @hist = ('?');
  545. $SIG{'INT'} = "DB::catch";
  546. $deep = 100;        # warning if stack gets this deep
  547. $window = 10;
  548. $preview = 3;
  549.  
  550. @stack = (0);
  551. @ARGS = @ARGV;
  552. for (@ARGS) {
  553.     s/'/¥¥'/g;
  554.     s/(.*)/'$1'/ unless /^-?[¥d.]+$/;
  555. }
  556.  
  557. if (-f $rcfile) {
  558.     do ':$rcfile';
  559. }
  560. elsif (-f "$ENV{'MPW'}$rcfile") {
  561.     do "$ENV{'MPW'}$rcfile";
  562. }
  563. elsif (-f "$ENV{'HOME'}$rcfile") {
  564.     do "$ENV{'HOME'}$rcfile";
  565. }
  566.  
  567. 1;
  568.